/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.nju.software.greencreditsystem.service;

import cn.edu.nju.software.greencreditsystem.DAO.FinancialFactorDAO;
import cn.edu.nju.software.greencreditsystem.DAO.FinancialScoreDAO;
import cn.edu.nju.software.greencreditsystem.DAO.PredictionDAO;
import cn.edu.nju.software.greencreditsystem.DTO.OutputFinancialFactorDTO;
import cn.edu.nju.software.greencreditsystem.domain.FinancialFactor;
import cn.edu.nju.software.greencreditsystem.domain.FinancialScore;
import cn.edu.nju.software.greencreditsystem.domain.Prediction;
import cn.edu.nju.software.greencreditsystem.factory.DAOFactory;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：韩天
 * 创建时间：2011-8-19
 * 类描述：财务模块相关的service封装类
 */
public class FinancialService {
    
    /**
     * 方法描述：取得所有的财务指标
     * param：
     * return：
     */
    public List<FinancialFactor> get_AllFinancialFactor(){
        FinancialFactorDAO ffDAO = DAOFactory.getFinancialFactorDAOInstance();
        return ffDAO.getAll();
    }
    
    /**
     * 方法描述：取得所有财务指标的概况
     * param：
     * return：factorDTOLIst——所有指标概况的list(前台显示)
     */
    public List<OutputFinancialFactorDTO> lookup_AllFinancialFactor(int enterpriseId){
        List<OutputFinancialFactorDTO> factorDTOLIst = new ArrayList<OutputFinancialFactorDTO>();
        //取得所有财务指标
        List<FinancialFactor> factorList = get_AllFinancialFactor();
        FinancialScoreDAO fsDAO = DAOFactory.getFinancialScoreDAOInstance();
        
        //取得每个财务指标的各项衡量数值
        for(int i=0; i<factorList.size(); i++){
            OutputFinancialFactorDTO factorDTO = new OutputFinancialFactorDTO();
            int factorId = factorList.get(i).getId();
            factorDTO.setFactorName(factorList.get(i).getName());
            factorDTO.setLatestValue(fsDAO.getLatest(enterpriseId, factorId).getValue());
            factorDTO.setUpdownValue(fsDAO.getIncreaseNumber(enterpriseId, factorId));
            factorDTO.setUpdownMargin(fsDAO.getIncreaseMargin(enterpriseId, factorId));
            factorDTO.setMaxValue(fsDAO.getMax(enterpriseId, factorId));
            factorDTO.setMinValue(fsDAO.getMin(enterpriseId, factorId));
            factorDTOLIst.add(factorDTO);
        }
        return factorDTOLIst;
    }
    
    /**
     * 方法描述：取得某个特定财务指标的概况
     * param：
     * return：factorDTO——某个指标概况(前台显示)
     */
    public OutputFinancialFactorDTO lookup_A_FinancialFactor(int enterpriseId, int factorId){
        FinancialFactorDAO ffDAO = DAOFactory.getFinancialFactorDAOInstance();
        FinancialScoreDAO fsDAO = DAOFactory.getFinancialScoreDAOInstance();
        FinancialFactor factor = ffDAO.getByID(factorId);
        //填满factorDTO的各项衡量数值
        OutputFinancialFactorDTO factorDTO = new OutputFinancialFactorDTO();
        factorDTO.setFactorName(factor.getName());
        factorDTO.setLatestValue(fsDAO.getLatest(enterpriseId, factorId).getValue());
        factorDTO.setUpdownValue(fsDAO.getIncreaseNumber(enterpriseId, factorId));
        factorDTO.setUpdownMargin(fsDAO.getIncreaseMargin(enterpriseId, factorId));
        factorDTO.setMaxValue(fsDAO.getMax(enterpriseId, factorId));
        factorDTO.setMinValue(fsDAO.getMin(enterpriseId, factorId));
        
        return factorDTO;
    }
    
    /**
     * 方法描述：取得某个财务指标的历史记录
     * param：
     * return：
     */
    public List<FinancialScore> get_FactorHistory(int enterpriseId, int factorId){
        FinancialScoreDAO fsDAO = DAOFactory.getFinancialScoreDAOInstance();
        return fsDAO.getLatest(enterpriseId, factorId, 0);
    }
    
    /**
     * 方法描述：取得某个财务指标的折线图
     * param：
     * return：
     */
    public BufferedImage get_FactorImage(int enterpriseId, int factorId){
        FinancialFactorDAO ffDAO = DAOFactory.getFinancialFactorDAOInstance();
        return ffDAO.drawImage(enterpriseId, factorId);
    }
    
    /**
     * 方法描述：取得某个企业的破产预警
     * param：
     * return：
     */
    public Prediction get_Prediction(int enterpriseId){
        PredictionDAO pDAO = DAOFactory.getPredictionDAOInstance();
        return pDAO.getLatest(enterpriseId);
    }
    
    /**
     * 方法描述：取得某个企业的财务总评
     * param：
     * return：
     */
    public double get_Total(int enterpriseId){
        FinancialScoreDAO fcDAO = DAOFactory.getFinancialScoreDAOInstance();
        return fcDAO.getLatestTotalScore(enterpriseId);
    }
}
